/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tnviet.proj.jna.textgroup;

/**
 *
 * @author Administrator
 */
public class IntegerValidator implements FieldValidator
{
	public final static int NEVER = 0;
	public final static int OPTIONAL = 1;
	public final static int ALWAYS = 2;

	private int minimum;
	private int maximum;
	private int rangeCheckDigits;
	private int signRequired;

	protected String message;

	/*
	**  Convenience constructor - effectively no range checking is done
	*/
	public IntegerValidator()
	{
		this(Integer.MIN_VALUE, Integer.MAX_VALUE, 0);
	}

	/*
	**  Convenience constructor - specify range checking
	**/
	public IntegerValidator(int minimum, int maximum)
	{
		this(minimum, maximum, 0);

		int min = Integer.toString( Math.abs(minimum) ).length();
		int max = Integer.toString( Math.abs(maximum) ).length();
		startRangeCheckAt( Math.min(min, max) );
	}

	/*
	**  Main contructor for creating this class.
	**  minimum - the minimum value allowed in this field
	**  maximum - the maximum value allowed in this field
	**  rangeCheckDigits - start range checking when this number of digits
	**		have been entered
	*/
	public IntegerValidator(int minimum, int maximum, int rangeCheckDigits)
	{
		this.minimum = minimum;
		this.maximum = maximum;
		startRangeCheckAt( rangeCheckDigits );;
	}

	public boolean isValid(String text)
	{
		//  Set variables used in later tests

		int multiplier = 1;
		boolean signEntered = false;
		char firstCharacter = text.charAt(0);

		if (firstCharacter == '+'
		||  firstCharacter == '-')
		{
			signEntered = true;
			text = text.substring(1);  // remove sign from text to validate

			if (firstCharacter == '-')
				multiplier = -1;
		}

		//  Validate the sign

		if (signRequired == NEVER
		&&  signEntered)
		{
			message = "Field must not contain a sign (+ or -)";
			return false;
		}

		if (signRequired == ALWAYS
		&&  !signEntered)
		{
			message = "Field must contain a sign (+ or -)";
			return false;
		}

		//  Only a sign has been entered, no number to validate

		if (text.length() == 0)
			return true;

		//  Check for a valid integer

		int value;

		try
		{
			value = Integer.parseInt( text ) * multiplier;
		}
		catch (Exception e)
		{
			message = "Field must contain a number";
			return false;
		}

		//  Do a range check on the value
		//  (when enough digits have been entered)

		if (text.length() < rangeCheckDigits)
		{
			return true;
		}
		else
		{
    		if (value < minimum
    		||  value > maximum)
    		{
    			message = "Field must contain a number in the range "
    					+ minimum + " : " + maximum;
    			return false;
    		}
    		else
    			return true;
    	}
	}

	public String getMessage()
	{
		return message;
	}

	/*
	**	Start range checking only when the specified number of digits
	**  have been entered.
	**  (For example, range check a year after 4 digits have been entered)
	*/
	public void startRangeCheckAt(int rangeCheckDigits)
	{
		this.rangeCheckDigits = rangeCheckDigits;
	}

	/*
	**  Indicates whether a leading +/- sign is required when entering integer
	**  NEVER - entering a sign will result in an error (default)
	**  OPTIONAL - sign is optional
	**  ALWAYS - a sign must be entered or an error will result
	*/
	public void setSignRequired(int signRequired)
	{
		this.signRequired = signRequired;
	}
}